package com.wantupai.mvp.framework.base.adapter;

import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

/**
 * @author :Created by zhangqing on 2018/11/10 10:19
 * @description :
 * @email :1423118197@qq.com
 * @classpath : com.football.wealth.mvp.framework.base.adapter.FragmentAdapter
 */
public abstract class FragmentAdapter<Item, T extends Fragment> extends FragmentStatePagerAdapter
{
    private List<Item> mItems = new ArrayList<Item>();
    public List<T> mList = new ArrayList<T>();
    private T mCurrentFragment;

    public FragmentAdapter(FragmentManager fm)
    {
        this(fm, null, null);
    }

    public FragmentAdapter(FragmentManager fm, List<Item> items)
    {
        this(fm, null, items);
    }

    public FragmentAdapter(FragmentManager fm, List<T> ts, List<Item> items)
    {
        super(fm);
        if (items != null)
        {
            mItems.clear();
            mItems.addAll(items);
        }
        if (ts != null)
        {
            mList.clear();
            mList.addAll(ts);
        }
    }

    public final CharSequence getPageTitle(int position)
    {
        Item item = mItems.get(position);
        if (item instanceof String)
        {
            return (String) item;
        }
        return getPageTitle(item);
    }

    @Override
    public final Fragment getItem(int i)
    {
        T t = null;
        if (mList.size() > i)
        {
            t = mList.get(i);
        }
        if (t != null)
        {
            return t;
        }
        t = getFragment(mItems.get(i));
        if (t != null)
        {
            return t;
        }
        t = getFragment(i);
        return t;
    }

    @Override
    public final int getCount()
    {
        return mItems.size();
    }

    @Override
    public final Object instantiateItem(ViewGroup container, int position)
    {
        T fragment = (T) super.instantiateItem(container, position);
        return fragment;
    }

    @Override
    public final void setPrimaryItem(ViewGroup container, int position, Object object)
    {
        mCurrentFragment = (T) object;
        super.setPrimaryItem(container, position, object);
    }

    public final T getCurrentFragment()
    {
        return mCurrentFragment;
    }

    public final void update(List<Item> items, boolean isDownRefresh)
    {
        if (isDownRefresh)
        {
            mItems.clear();
        }
        mItems.addAll(items);
        notifyDataSetChanged();
    }

    public final void remove(int index)
    {
        if (index >= 0 && index <= mItems.size())
        {
            mItems.remove(index);
        }
        else
        {
            throw new ArrayIndexOutOfBoundsException("数组下标越界 index=" + index + "超出了范围");
        }
    }

    public final void addSatrt(Item item)
    {
        add(item, 0);
    }

    public final void addEnd(Item item)
    {
        add(item, mItems.size());
    }

    public final void add(Item item, int index)
    {
        mItems.add(index, item);
        notifyDataSetChanged();
    }


    public T getFragment(Item item)
    {
        return null;
    }

    public T getFragment(int index)
    {
        return null;
    }

    public CharSequence getPageTitle(Item item)
    {
        return null;
    }

    @Override
    public int getItemPosition(@NonNull Object object)
    {
        return POSITION_NONE;
    }
}
